package com.xl.sst.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xl.sst.biz.DesiredBiz;
import com.xl.sst.vo.Desired;
import com.xl.sst.vo.Page;
import com.xl.sst.vo.TableNum;



/*
 * 商品处理层
 * yue
 */
@Controller
@RequestMapping("/view")
public class DesiredController {
	@Resource
	private DesiredBiz desiredBiz;
	public DesiredBiz getDesiredBiz() {
		return desiredBiz;
	}
	public void setDesiredBiz(DesiredBiz desiredBiz) {
		this.desiredBiz = desiredBiz;
	}

	/*
	 * 添加需求
	 * 测试 
	 * http://localhost:8080/SST4/view/xuqiuadd.do?xtitle=weew
	 * 需要xtitke字段不能为空
	 */
	@RequestMapping("/xuqiuadd.do")
	public @ResponseBody Map<String,Object> add(Desired d){
		Map<String,Object> m=new HashMap<String, Object>();
		if(desiredBiz.add(d)){
			m.put("xuqiuadd", "添加成功！");
		}else{
			m.put("xuqiuadd", "添加失败！");
		}
		return m;			
	}
	/*
	 * 修改需求
	 * 测试
	 * http://localhost:8080/SST4/view/xuqiuupdate.do?xtitle=weew&id=1
	 */
	@RequestMapping("/xuqiuupdate.do")
	public @ResponseBody Map<String,Object> update(Desired d){
		Map<String,Object> m=new HashMap<String, Object>();
		if(desiredBiz.update(d)){
			m.put("xuqiuupdate", "修改成功！");
		}else{
			m.put("xuqiuupdate", "修改失败！");
		}
		return m;			
	}
	/*
	 * 删除需求
	 * 测试
	 * http://localhost:8080/SST4/view/xuqiudelete.do?id=2
	 */
	@RequestMapping("/xuqiudelete.do")
	public @ResponseBody Map<String,Object> delete(Desired d){
		Map<String,Object> m=new HashMap<String, Object>();
		int i=desiredBiz.delete(d);
		if(i>=1){
			m.put("xuqiudelete", "成功删除"+i+"条需求！");
		}else{
			m.put("xuqiudelete", "删除失败！");
		}
		return m;			
	}
	/*
	 * 查询需求 根据用户id查询
	 * 我发布的需求
	 * 测试
	 * http://localhost:8080/SST4/view/xuqiuselectbyuser.do?creatUser=1
	 */
	@RequestMapping("/xuqiuselectbyuser.do")
	public @ResponseBody Map<String,Object> selectByUser(Desired d){
		Map<String,Object> m=new HashMap<String, Object>();
		m.put("xuqiuselectbyuser", desiredBiz.selectByUser(d));
		return m;			
	}
	/*
	 * 查询所有需求 需求展示
	 * 测试
	 * http://localhost:8080/SST4/view/xuqiuselectall.do?index=1&row=2&category=1
	 */
	@RequestMapping("/xuqiuselectall.do")
	public @ResponseBody Map<String,Object> selectAll(Page page){
		Map<String,Object> m=new HashMap<String, Object>();
		//当前页
		int nowPage =1;
		//每页条数
		int rows=2;
		//总页数
		int sumPage=1;
		//获取页面当前页并传递
		if(page !=null){
			nowPage=page.getIndex();
		}		
		//获取分页数并传递
		int count=desiredBiz.getCount(page);
		if(count%rows==0){
			sumPage=count/rows;
		}else{
			sumPage=count/rows+1;
		}				
		Page p=new Page();
		p.setIndex(rows*(nowPage-1));
		p.setRow(rows);
		p.setCategory(page.getCategory());
		//获取需求
		List<Desired> list=desiredBiz.selectAll(p);
		TableNum t=new TableNum();
		t.setPage(nowPage);	
		t.setTotalPage(sumPage);
		t.setTotalSize(count);
		t.setList(list);
		m.put("xuqiuselectall", t);
		return m;			
	}
}
